// JavaScript Document
$(document).ready(function(e) {
	$('div.tab-content').css('height',(screen.availHeight-265));
	$('#tgl_trans1').datepicker();
	$('#tgl_trans1').datepicker("option","dateFormat","dd/mm/yy");
	tglNow('#tgl_trans1');
	$('#tgl_trans').datepicker();
	$('#tgl_trans').datepicker("option","dateFormat","dd/mm/yy");
	tglNow('#tgl_trans');
	_get_member('R');
	_get_member('D');
	_get_member2('R');
	_get_member2('D');
	$('#frm1').validationEngine();$('#frm2').validationEngine();
	$('#no_member').live('change',function(){($(this).val().length > 2)?_get_detail($(this).val()):'';});
	$('#nm_member').live('change',function(){($(this).val().length > 3)?_get_detail($(this).val()):'';});
	$('#no_member2').live('change',function(){($(this).val().length > 2)?_get_detail2($(this).val()):'';});
	$('#nm_member2').live('change',function(){($(this).val().length > 3)?_get_detail2($(this).val()):'';});
	$('#b_Save').click(function(){($('#frm1').validationEngine('validate'))? _set_penarikan():'';});
	$('#b_Saved').click(function(){ ($('#frm2').validationEngine('validate'))?_setpenarikan_total():'';})
	formatAngka('jml_simpanan');
	formatAngka('sp_pokok');
	formatAngka('sp_wajib');
	formatAngka('sp_sukarela');
	formatAngka('sp_pinjbar');
	formatAngka('sp_pintun');
	formatAngka('sp_pinbar_nk');
	$('#jml_simpanan').focusout(function(){ $('#ket_trans').val('Penarikan Simpanan - '+$('#nm_member').val());});
	$('#cara_bayar2').change(function(r)
	{
		$('#ket_trans2').text('');
		$('#ket_trans2').text('Penarikan Total Simpanan - '+ $('#nm_member2').val()+'- Non aktif anggota');	
	})
});

function _get_member(tp)
{
	$.post('getakunmember',{'st':tp},function(result){
		(tp=='R')?$('#no_member').typeahead({source:$.parseJSON(result)}):
		$('#nm_member').typeahead({source:$.parseJSON(result)});});
		
}
function _get_member2(tp)
{
	$.post('getakunmember',{'st':tp,'stat':'N'},function(result){
		(tp=='R')?$('#no_member2').typeahead({source:$.parseJSON(result)}):
		$('#nm_member2').typeahead({source:$.parseJSON(result)});});
		
}
/**get detail member name or member id*/
function _get_detail(fld)
{
	$.post('get_akun_detail',{'id':fld},function(result)
	{
		var r=$.parseJSON(result);
		$('#nm_member').val(r.nm_member);
		$('#no_member').val(r.id_reg);
		_get_saldo_simpanan(r.id_reg)
		})
}
function _get_detail2(fld)
{
	$.post('get_akun_detail',{'id':fld},function(result)
	{
		var r=$.parseJSON(result);
		//$('#nm_member2').val(r.nm_member);
		$('#no_member2').val(r.id_reg);
		_getsaldosimpanan(r.id_reg)
		})
}
function _get_saldo_simpanan(id)
{
	$.post('getsaldosimpanan',{'id_member':id},
	function(result)
	{
		$('#saldo_simpanan').attr('readonly','readonly').val(result).priceFormat();
	})
}
function _getsaldosimpanan(id)
{
	$.post('getdetailsimpanan',{'id_member':id},function(result)
	{
		var r=$.parseJSON(result);
		$('#sp_pokok').attr('readonly','readonly').val(r.simpanan_pokok).priceFormat();
		$('#sp_wajib').attr('readonly','readonly').val(r.saldo_wajib).priceFormat();
		$('#sp_sukarela').attr('readonly','readonly').val(r.saldo_sukarela).priceFormat();
		$('#sp_pinbar').attr('readonly','readonly').val(r.saldo_pinjaman_barang).priceFormat();
		$('#sp_pintun').attr('readonly','readonly').val(r.saldo_pinjaman_uang).priceFormat();
		var total_s=parseFloat(r.simpanan_pokok)+parseFloat(r.saldo_wajib)+parseFloat(r.saldo_sukarela)
		var total_p=(parseInt(r.saldo_pinjaman_barang)+parseFloat(r.saldo_pinjaman_uang));
		$('#jml_penarikan').attr('readonly','readonly').val(total_s-total_p).priceFormat({'allowNegative':'0'});
	})
}
function _set_penarikan()
{
	$.post('setpenarikan',$('#frm1').serialize(),function(result)
	{
		$('#result').html(result).show().fadeOut(5000);
		$('#d_Save').click();tglNow('#tgl_trans');
	})
}

function _setpenarikan_total()
{
	var tp=$('#jml_penarikan').val();
	if(parseFloat(tp)<0)
	{ 
		jAlert("Pinjaman masih lebih besar dari simpanan.\n Input pelunasan terlebih dahulu");
	}else{
		if($('#cara_bayar2').val()=='Transfer')
		{
			$.post('getakundropdown',{'idparent':'113'},function(rst){$("#popup_prompt").html(rst);})
			jPrompts("Pilih Kontra akun Bank",'','Kontra Akun',function(e)
			{
				if(e)
				{
					$.post('settariktotal',$('#frm2').serialize()+'&id_akun2='+e,function(result)
					{
						$('#result').html(result).show().fadeOut(5000);
						$('#d_Saved').click();tglNow('#tgl_trans');
					})
				}else{
					jAlert("Pilih dulu akun kontranya")
				}
			})
		}else{
			$.post('settariktotal',$('#frm2').serialize(),function(result)
			{
				$('#result').html(result).show().fadeOut(5000);
				$('#d_Saved').click();tglNow('#tgl_trans');
			})
		}
	}

}